package com.edu.modules.baseinfo.service;

import com.alibaba.fastjson.JSONObject;
import com.edu.modules.baseinfo.dao.WXDao;
import com.edu.modules.baseinfo.vo.ResultVo;
import com.edu.modules.comm.WXData;
import com.sari.common.service.BaseService;
import com.sari.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


/**
 * Created by kwj on 2016/7/15.
 */
@Service
public class WXService extends BaseService {

	@Autowired
	private WXDao wxDao;

	public void updateToken(String token){

		wxDao.updateTokenTicket(token, DateUtils.getDateTime(),"1");
	}

	public void updateTicket(String ticket){

		wxDao.updateTokenTicket(ticket, DateUtils.getDateTime(),"2");
	}

	public ResultVo saveOpenId(JSONObject param){

		String openId = param.getString("openId");
		String parentOpenId = param.getString("parentOpenId");
		String createTime = DateUtils.getDateTime();

		wxDao.saveOpenId(openId,parentOpenId,createTime);
		return new ResultVo(true,"success");
	}

	public JSONObject getShareData(String url) {

		String timeStamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳
		String nonceStr = UUID.randomUUID().toString().replace("-", "").substring(0, 16);//随机字符串
		String signature = WXData.getSignature(url,wxDao.getTokenTicket("2"),timeStamp,nonceStr);

		JSONObject result= new JSONObject();
		result.put("timeStamp",timeStamp);
		result.put("nonceStr", nonceStr);
		result.put("signature", signature);

		return result;
	}

	public JSONObject getWxData(){

		JSONObject result= new JSONObject();
		result.put("token",wxDao.getTokenTicket("1"));
		result.put("ticket", wxDao.getTokenTicket("2"));

		return result;
	}

}
